--    Program: 		CAPARIMP_RECEIPT_MISC_DIST.sql
--    Description:  Import AR RECEIPT MISC DIST information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         2-Feb-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
--whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
   
   l_misc_cash_distribution_id	number;
                    		                 		
   cursor cur_ar_receipt_misc_dist is
      select * from CRST_AR_MISC_CASH_DISTRIBUTION
      for update;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_ar_receipt_misc_dist loop   	
     l_err_msg_tmp := '';
     
     SELECT AR_MISC_CASH_DISTRIBUTIONS_S.NEXTVAL 
     INTO l_misc_cash_distribution_id
     FROM DUAL;
     
     update CRST_AR_MISC_CASH_DISTRIBUTION
     set    misc_cash_distribution_id_n = l_misc_cash_distribution_id
     where  misc_cash_distribution_id = cur.misc_cash_distribution_id;
     
     begin
       -- import data
       insert into AR_MISC_CASH_DISTRIBUTIONS_ALL
       (
          MISC_CASH_DISTRIBUTION_ID       ,
          LAST_UPDATED_BY                 ,
          LAST_UPDATE_DATE                ,
          LAST_UPDATE_LOGIN               ,
          CREATED_BY                      ,
          CREATION_DATE                   ,
          CASH_RECEIPT_ID                 ,
          CODE_COMBINATION_ID             ,
          SET_OF_BOOKS_ID                 ,
          GL_DATE                         ,
          PERCENT                         ,
          AMOUNT                          ,
          COMMENTS                        ,
          GL_POSTED_DATE                  ,
          APPLY_DATE                      ,
          ATTRIBUTE_CATEGORY              ,
          ATTRIBUTE1                      ,
          ATTRIBUTE2                      ,
          ATTRIBUTE3                      ,
          ATTRIBUTE4                      ,
          ATTRIBUTE5                      ,
          ATTRIBUTE6                      ,
          ATTRIBUTE7                      ,
          ATTRIBUTE8                      ,
          ATTRIBUTE9                      ,
          ATTRIBUTE10                     ,
          POSTING_CONTROL_ID              ,
          ACCTD_AMOUNT                    ,
          ATTRIBUTE11                     ,
          ATTRIBUTE12                     ,
          ATTRIBUTE13                     ,
          ATTRIBUTE14                     ,
          ATTRIBUTE15                     ,
          PROGRAM_APPLICATION_ID          ,
          PROGRAM_ID                      ,
          PROGRAM_UPDATE_DATE             ,
          REQUEST_ID                      ,
          USSGL_TRANSACTION_CODE          ,
          USSGL_TRANSACTION_CODE_CONTEXT  ,
          CREATED_FROM                    ,
          REVERSAL_GL_DATE                ,
          ORG_ID                          ,
          MRC_GL_POSTED_DATE              ,
          MRC_POSTING_CONTROL_ID          ,
          MRC_ACCTD_AMOUNT                 
       )
       values
       (
            l_misc_cash_distribution_id         ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.LAST_UPDATED_BY),
            cur.LAST_UPDATE_DATE                ,
            cur.LAST_UPDATE_LOGIN               ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
            cur.CREATION_DATE                   ,
            CAPARIMP_MAPPING.GetCashReceiptIDMapping(cur.CASH_RECEIPT_ID),
            CAPBRIMP_MAPPING.GetCCIDMapping(cur.CODE_COMBINATION_ID),
            CAPBRIMP_MAPPING.GetSOBIDMapping(cur.SET_OF_BOOKS_ID),
            cur.GL_DATE                         ,
            cur.PERCENT                         ,
            cur.AMOUNT                          ,
            cur.COMMENTS                        ,
            cur.GL_POSTED_DATE                  ,
            cur.APPLY_DATE                      ,
            cur.ATTRIBUTE_CATEGORY              ,
            cur.ATTRIBUTE1                      ,
            cur.ATTRIBUTE2                      ,
            cur.ATTRIBUTE3                      ,
            cur.ATTRIBUTE4                      ,
            cur.ATTRIBUTE5                      ,
            cur.ATTRIBUTE6                      ,
            cur.ATTRIBUTE7                      ,
            cur.ATTRIBUTE8                      ,
            cur.ATTRIBUTE9                      ,
            cur.ATTRIBUTE10                     ,
            cur.POSTING_CONTROL_ID              ,
            cur.ACCTD_AMOUNT                    ,
            cur.ATTRIBUTE11                     ,
            cur.ATTRIBUTE12                     ,
            cur.ATTRIBUTE13                     ,
            cur.ATTRIBUTE14                     ,
            cur.ATTRIBUTE15                     ,
            cur.PROGRAM_APPLICATION_ID          ,
            cur.PROGRAM_ID                      ,
            cur.PROGRAM_UPDATE_DATE             ,
            cur.REQUEST_ID                      ,
            cur.USSGL_TRANSACTION_CODE          ,
            cur.USSGL_TRANSACTION_CODE_CONTEXT  ,
            cur.CREATED_FROM                    ,
            cur.REVERSAL_GL_DATE                ,
            CAPBRIMP_MAPPING.GetOrgMapping(cur.ORG_ID),
            cur.MRC_GL_POSTED_DATE              ,
            cur.MRC_POSTING_CONTROL_ID          ,
            cur.MRC_ACCTD_AMOUNT                                       
       );  
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp = '' then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;
   end loop;

   if l_rejected_count <> 0 then
   	 dbms_output.put_line('1');
   	 commit;
   else
     dbms_output.put_line('2');
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPARIMP_RECEIPT_MISC_DIST', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
